package cn.stylefeng.guns.modular.packreview.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.base.shiro.ShiroUser;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.model.result.JHZYJDResult;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.order.model.params.OrderPutParam;
import cn.stylefeng.guns.modular.packreview.entity.PackReview;
import cn.stylefeng.guns.modular.packreview.model.params.PackReviewParam;
import cn.stylefeng.guns.modular.packreview.model.result.PackReviewResult;
import cn.stylefeng.guns.modular.packreview.service.PackReviewService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.model.result.DictResult;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import static cn.stylefeng.guns.sys.core.shiro.ShiroKit.getUser;


/**
 * 打包复核控制器
 *
 * @author wp
 * @Date 2025-07-05 11:40:28
 */
@Controller
@RequestMapping("/packReview")
public class PackReviewController extends BaseController {

    private String PREFIX = "/packReview";

    @Autowired
    private PackReviewService packReviewService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicCustomerService basicCustomerService;
    @Autowired
    private BasicWarehouseService basicWarehouseService;

    private Lock lock = new ReentrantLock();
    /**
     * 跳转到主页面
     *
     * @author wp
     * @Date 2025-07-05
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/packReview.html";
    }

    /**
     * 新增页面
     *
     * @author wp
     * @Date 2025-07-05
     */
    @RequestMapping("/add")
    public String add(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();

        //查询可选择客户
        model.addAttribute("customerlist", basicCustomerService.findAvailableList(user.getDeptId(),user.getDeliverId(),user.getOperateId()));
        //查询可选择仓库
        model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/packReview_add.html";
    }


    /**
     * 编辑页面
     *
     * @author wp
     * @Date 2025-07-05
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/packReview_edit.html";
    }

    /**
     * 新增接口
     *
     * @author wp
     * @Date 2025-07-05
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(PackReviewParam packReviewParam) {

        ShiroUser user = getUser();
        packReviewParam.setDeptId(user.getDeptId());
        List<PackReviewResult> packReviewResults = this.packReviewService.findListBySpec(packReviewParam);

        int count = packReviewResults.size();
        if (count > 0) {
            return ResponseData.error("该客户已存在打包复核记录，请勿重复添加！");
        }
        this.packReviewService.add(packReviewParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author wp
     * @Date 2025-07-05
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(PackReviewParam packReviewParam) {
        this.packReviewService.update(packReviewParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author wp
     * @Date 2025-07-05
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(PackReviewParam packReviewParam) {
        this.packReviewService.delete(packReviewParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author wp
     * @Date 2025-07-05
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(PackReviewParam packReviewParam) {
        PackReview detail = this.packReviewService.getById(packReviewParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author wp
     * @Date 2025-07-05
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(PackReviewParam packReviewParam,String condition ) {
        return this.packReviewService.findPageBySpec(packReviewParam,condition);
    }


    /**
     * 导入页面
     */
    @RequestMapping("/toimportExcel")
    public String toimportExcel(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        //查询可选择客户
        model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
        //查询可选择仓库
        model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/packReview_import.html";
    }





    @RequestMapping(value = "/importPackReviewExcel" , method = {RequestMethod.POST} )
    @ResponseBody
    public ResponseData importPackReviewExcel(@RequestPart("file") MultipartFile file, PackReviewParam packReviewParam)  {
        ResponseData data = new ResponseData();
        lock.lock();//获取锁
        try {
            data = packReviewService.getExceldata(file,packReviewParam,data);//处理导入数据
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();//释放锁
        }
        return data;
    }

    /**
     * 复核作业进度
     *
     * @author wp
     * @Date 2025-07-05
     */
    @RequestMapping("fhzyjd")
    public String fhzyjd() {
        return PREFIX + "/fhzyjd.html";
    }


    @ResponseBody
    @RequestMapping("getFhzyjdList")
    public LayuiPageInfo getFhzyjdList(String currentDate ) {
        if(currentDate == null || currentDate.equals("")) {
            currentDate = DateUtil.getDate();
        }
        LayuiPageInfo pageInfo = new LayuiPageInfo();
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        List<JHZYJDResult> jhzyjdListByDeptId = packReviewService.findFhzyjdListByDeptId(deptId, currentDate);

        pageInfo.setData(jhzyjdListByDeptId);
        return pageInfo;
    }

}


